cmake_minimum_required(VERSION 3.0.2)
project(pc_udp_streamer)

# 编译选项
add_compile_options(-std=c++14 -O3 -Wall -Wextra -pthread)

# ------------------------
# Catkin / 依赖
# ------------------------
find_package(catkin REQUIRED COMPONENTS
  roscpp
  sensor_msgs
  pcl_ros
  pcl_conversions
  message_filters
  tf2_ros
  tf2_eigen
)

find_package(PCL REQUIRED)

# 仅使用 /usr/local 安装的 Draco（方案B）
# 若找不到，请先在 /usr/local 下重新安装：BUILD_SHARED_LIBS=ON, PIC=ON
find_path(DRACO_INCLUDE_DIR draco/compression/decode.h
  PATHS /usr/local/include
  NO_DEFAULT_PATH
)
find_library(DRACO_LIBRARY draco
  PATHS /usr/local/lib
  NO_DEFAULT_PATH
)
if(NOT DRACO_INCLUDE_DIR OR NOT DRACO_LIBRARY)
  message(FATAL_ERROR
    "Draco not found in /usr/local. Please build & install it there:\n"
    "  git clone https://github.com/google/draco.git && cd draco && mkdir build && cd build && \n"
    "  cmake .. -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON -DCMAKE_POSITION_INDEPENDENT_CODE=ON && \n"
    "  make -j$(nproc) && sudo make install && sudo ldconfig")
endif()

include_directories(
  ${catkin_INCLUDE_DIRS}
  ${PCL_INCLUDE_DIRS}
  ${DRACO_INCLUDE_DIR}
)
add_definitions(${PCL_DEFINITIONS})

# 可选：fast_gicp（存在则启用加速 GICP/VGICP）
find_package(fast_gicp QUIET)

# 可选：Zstd（系统库）
find_library(ZSTD_LIB zstd)
if(ZSTD_LIB)
  message(STATUS "Found zstd: ${ZSTD_LIB}")
else()
  message(WARNING "libzstd not found; RAW mode only (ignored if using Draco)")
endif()

catkin_package(
  CATKIN_DEPENDS roscpp sensor_msgs pcl_ros pcl_conversions message_filters tf2_ros tf2_eigen
)

# ------------------------
# 可执行文件
# ------------------------

# 发送端（Draco）
add_executable(pc_udp_sender src/pc_udp_sender.cpp)
target_link_libraries(pc_udp_sender
  ${catkin_LIBRARIES}
  ${DRACO_LIBRARY}
  pthread
)
# 若你保留了兼容 Zstd 的代码宏，这里也链接（不会影响 Draco）
if(ZSTD_LIB)
  target_compile_definitions(pc_udp_sender PRIVATE HAS_ZSTD=1)
  target_link_libraries(pc_udp_sender ${ZSTD_LIB})
endif()
# 运行时 rpath，确保能在 /usr/local/lib 找到 libdraco.so
set_target_properties(pc_udp_sender PROPERTIES
  BUILD_RPATH "/usr/local/lib"
  INSTALL_RPATH "/usr/local/lib"
)

# 接收端（Draco）
add_executable(pc_udp_receiver src/pc_udp_receiver.cpp)
target_link_libraries(pc_udp_receiver
  ${catkin_LIBRARIES}
  ${DRACO_LIBRARY}
  pthread
)
if(ZSTD_LIB)
  target_compile_definitions(pc_udp_receiver PRIVATE HAS_ZSTD=1)
  target_link_libraries(pc_udp_receiver ${ZSTD_LIB})
endif()
set_target_properties(pc_udp_receiver PROPERTIES
  BUILD_RPATH "/usr/local/lib"
  INSTALL_RPATH "/usr/local/lib"
)

# 多源拼接（TF/GICP）
add_executable(pc_multi_merger src/pc_multi_merger.cpp)
target_link_libraries(pc_multi_merger
  ${catkin_LIBRARIES}
  ${PCL_LIBRARIES}
)
if(fast_gicp_FOUND)
  target_compile_definitions(pc_multi_merger PRIVATE HAS_FAST_GICP=1)
  target_link_libraries(pc_multi_merger fast_gicp)
endif()

# 纯 TF 拼接（极简）
add_executable(pc_tf_merger src/pc_tf_merger.cpp)
target_link_libraries(pc_tf_merger ${catkin_LIBRARIES})

# ------------------------
# 提示
# ------------------------
message(STATUS "Using Draco include: ${DRACO_INCLUDE_DIR}")
message(STATUS "Using Draco library: ${DRACO_LIBRARY}")

